#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
bool status;
typedef struct Project
{
    long projectId;
    char projectName[999];
    short projectStatus; //0为未开始，1为进行中，2为已完成
    short projectAwards; //0为未获奖，1为1等奖(500)，2为2等奖(300)，3为三等奖(100)
    struct Project *nextProject;
} Project, *ProjectList;
typedef struct Thesis
{
    long thesisId;
    char thesisName[999];
    short thesisStatus; //0为未开始，1为进行中，2为已完成
    short thesisAwards; //0为未获奖，1国家级，2为省部级，3为市厅级,4为校级
    short thesisType;   //0为无，1为SCI(500)，2为EI(300)，3为核心(100)，4为一般(50)
    struct Thesis *nextThesis;
} Thesis, *ThesisList;
typedef struct Essay
{
    long essayId;
    char essayName[999];
    short essayType; //0为无，1为国家标准(500)，2为行业标准(300)，3为专辑(200)，4为学术专著(100)，5为其他（含译著）(50)
    struct Essay *nextEssay;
} Essay, *EssayList;
typedef struct User
{
    long length; //可以使用这个来记录长度

    long id;
    char name[10]; //姓名
    User *nextUser;
    Project *nextProject;
    Thesis *nextThesis;
    Essay *nextEssay;

} User, *UserList;
typedef struct Rank
{
    int score;
    char name[10];
    Rank *nextRank;
    Rank *lastRank;
} Rank, *RankList;
UserList InitUser()                                                                                                                                                                
{
    UserList head = (UserList)malloc(sizeof(User));
    RankList rankHead = (RankList)malloc(sizeof(Rank));
    //记录长度
    head->length = 0;
    head->nextUser = NULL;
    head->nextProject = NULL;
    head->nextThesis = NULL;
    head->nextEssay = NULL;
    head->id = 0;
    if (NULL == head)
    {
        printf("链表动态分配失败！程序终止！\n");
        exit(-1);
    }
    return head;
}
RankList InitRank()
{
    UserList head = (UserList)malloc(sizeof(User));
    RankList rankHead = (RankList)malloc(sizeof(Rank));
    rankHead->nextRank = NULL;
    rankHead->lastRank = NULL;
    rankHead->score = 0;
    if (NULL == rankHead)
    {
        printf("链表动态分配失败！程序终止！\n");
        exit(-1);
    }
    return rankHead;
}
bool AddUser(UserList head, long id, char name[10])
{
    if (head == NULL)
    {
        return false;
    }
    UserList user = (UserList)malloc(sizeof(User));
    UserList temp = (UserList)malloc(sizeof(User));
    user->id = id;
    strcpy(user->name, name);
    // essay->essayName[999] = essayName;
    if (head->nextUser == NULL)
    {

        head->nextUser = user;
        user->nextUser = NULL;
        //新加上
        user->nextProject = NULL;
        user->nextEssay = NULL;
        user->nextThesis = NULL;
    }
    else
    {
        temp = head->nextUser;
        while (temp->nextUser != NULL)
        {
            temp = temp->nextUser;
        }
        temp->nextUser = user;
        user->nextUser = NULL;

        //新加上
        user->nextProject = NULL;
        user->nextEssay = NULL;
        user->nextThesis = NULL;
    }
    head->length++;
    return true;
}
bool AddProject(UserList user, long projectId, char projectName[999], short projectStatus, short projectAwards)
{
    if (user == NULL)
    {
        return false;
    }
    ProjectList project = (ProjectList)malloc(sizeof(Project));
    ProjectList temp = (ProjectList)malloc(sizeof(Project));
    project->projectId = projectId;
    strcpy(project->projectName, projectName);

    project->projectStatus = projectStatus;
    project->projectAwards = projectAwards;
    if (user->nextProject == NULL)
    {
        user->nextProject = project;
        project->nextProject = NULL;
    }
    else
    {
        temp = user->nextProject;
        while (temp->nextProject != NULL)
        {
            temp = temp->nextProject;
        }
        temp->nextProject = project;
        project->nextProject = NULL;
    }
    return true;
}
bool AddThesis(UserList user, long thesisId, char thesisName[999], short thesisStatus, short thesisAwards, short thesisType)
{
    if (user == NULL)
    {
        return false;
    }
    ThesisList thesis = (ThesisList)malloc(sizeof(Thesis));
    ThesisList temp = (ThesisList)malloc(sizeof(Thesis));
    thesis->thesisId = thesisId;
    // thesis->thesisName[999] = thesisName[999];
    // thesis->thesisUserName[432] = thesisUserName[432];
    strcpy(thesis->thesisName, thesisName);
    thesis->thesisStatus = thesisStatus;
    thesis->thesisAwards = thesisAwards;
    thesis->thesisType = thesisType;
    if (user->nextThesis == NULL)
    {
        user->nextThesis = thesis;
        thesis->nextThesis = NULL;
    }
    else
    {
        temp = user->nextThesis;
        while (temp->nextThesis != NULL)
        {
            temp = temp->nextThesis;
        }
        temp->nextThesis = thesis;
        thesis->nextThesis = NULL;
    }
    return true;
}
bool AddEssay(UserList user, long essayId, char essayName[999], short essayType)
{
    if (user == NULL)
    {
        return false;
    }
    EssayList essay = (EssayList)malloc(sizeof(Essay));
    EssayList temp = (EssayList)malloc(sizeof(Essay));
    essay->essayId = essayId;
    strcpy(essay->essayName, essayName);
    essay->essayType = essayType;
    if (user->nextEssay == NULL)
    {
        user->nextEssay = essay;
        essay->nextEssay = NULL;
    }
    else
    {
        temp = user->nextEssay;
        while (temp->nextEssay != NULL)
        {
            temp = temp->nextEssay;
        }
        temp->nextEssay = essay;
        essay->nextEssay = NULL;
    }
    return true;
}
UserList SearchUser(UserList head, long id)
{
    UserList temp = (UserList)malloc(sizeof(User));
    if (head->nextUser == NULL || head == NULL)
    {
        status = 0;
        exit(-1);
    }
    else
    {
        temp = head->nextUser;
        if (temp->id == id)
        {
            status = 1;
            return temp;
        }
        while (temp->nextUser != NULL)
        {
            temp = temp->nextUser;
            if (temp->id == id)
            {
                status = 1;
                return temp;
            }
        }
    }
    status = 0;
    return temp;
}
ProjectList SearchProject(UserList user, long projectId)
{
    ProjectList temp = (ProjectList)malloc(sizeof(Project));
    if (user->nextUser == NULL || user == NULL)
    {
        status = 0;
        exit(-1);
    }
    else
    {
        temp = user->nextProject;
        if (temp->projectId == projectId)
        {
            status = 1;
            return temp;
        }
        while (temp->nextProject != NULL)
        {
            temp = temp->nextProject;
            if (temp->projectId == projectId)
            {
                status = 1;
                return temp;
            }
        }
    }
    status = 0;
    return temp;
}
ThesisList SearchThesis(UserList user, long thesisId)
{
    ThesisList temp = (ThesisList)malloc(sizeof(Thesis));
    if (user->nextUser == NULL || user == NULL)
    {
        status = 0;
        exit(-1);
    }
    else
    {
        temp = user->nextThesis;
        if (temp->thesisId == thesisId)
        {
            status = 1;
            return temp;
        }
        while (temp->thesisId != NULL)
        {
            temp = temp->nextThesis;
            if (temp->thesisId == thesisId)
            {
                status = 1;
                return temp;
            }
        }
    }
    status = 0;
    return temp;
}
EssayList SearchEssay(UserList user, long essayId)
{
    EssayList temp = (EssayList)malloc(sizeof(Essay));
    if (user->nextUser == NULL || user == NULL)
    {
        status = 0;
        exit(-1);
    }
    else
    {
        temp = user->nextEssay;
        if (temp->essayId == essayId)
        {
            status = 1;
            return temp;
        }
        while (temp->essayId != NULL)
        {
            temp = temp->nextEssay;
            if (temp->essayId == essayId)
            {
                status = 1;
                return temp;
            }
        }
    }
    status = 0;
    return temp;
}
bool DeleteUser(UserList head, long id)
{
    UserList lastTemp = (UserList)malloc(sizeof(User));
    UserList temp = (UserList)malloc(sizeof(User));
    if (head->nextUser == NULL || head == NULL)
    {
        status = 0;
        exit(-1);
    }
    else
    {
        temp = head->nextUser;
        if (temp->id == id)
        {
            head->nextUser = temp->nextUser;
            free(temp);
            head->length--;
            return true;
        }
        lastTemp = temp;
        while (temp->nextUser != NULL)
        {
            temp = temp->nextUser;
            if (temp->id == id)
            {
                lastTemp->nextUser = temp->nextUser;
                free(temp);
                head->length--;
                return true;
            }
            lastTemp = temp;
        }
    }
    return false;
}
bool DeleteProject(UserList user, long projectId)
{
    ProjectList lastTemp = (ProjectList)malloc(sizeof(Project));
    ProjectList temp = (ProjectList)malloc(sizeof(Project));
    if (user->nextUser == NULL || user == NULL)
    {
        status = 0;
        exit(-1);
    }
    else
    {
        temp = user->nextProject;
        if (temp->projectId == projectId)
        {
            user->nextProject = temp->nextProject;
            free(temp);
            // head->length--;
            return true;
        }
        lastTemp = temp;
        while (temp->nextProject != NULL)
        {
            temp = temp->nextProject;
            if (temp->projectId == projectId)
            {
                lastTemp->nextProject = temp->nextProject;
                free(temp);
                // head->length--;
                return true;
            }
            lastTemp = temp;
        }
    }
    return false;
}
bool DeleteThesis(UserList user, long thesisId)
{
    ThesisList lastTemp = (ThesisList)malloc(sizeof(Thesis));
    ThesisList temp = (ThesisList)malloc(sizeof(Thesis));
    if (user->nextUser == NULL || user == NULL)
    {
        status = 0;
        exit(-1);
    }
    else
    {
        temp = user->nextThesis;
        if (temp->thesisId == thesisId)
        {
            user->nextThesis = temp->nextThesis;
            free(temp);
            // head->length--;
            return true;
        }
        lastTemp = temp;
        while (temp->nextThesis != NULL)
        {
            temp = temp->nextThesis;
            if (temp->thesisId == thesisId)
            {
                lastTemp->nextThesis = temp->nextThesis;
                free(temp);
                // head->length--;
                return true;
            }
            lastTemp = temp;
        }
    }
    return false;
}
bool DeleteEssay(UserList user, long essayId)
{
    EssayList lastTemp = (EssayList)malloc(sizeof(Essay));
    EssayList temp = (EssayList)malloc(sizeof(Essay));
    if (user->nextUser == NULL || user == NULL)
    {
        status = 0;
        exit(-1);
    }
    else
    {
        temp = user->nextEssay;
        if (temp->essayId == essayId)
        {
            user->nextEssay = temp->nextEssay;
            free(temp);
            // head->length--;
            return true;
        }
        lastTemp = temp;
        while (temp->nextEssay != NULL)
        {
            temp = temp->nextEssay;
            if (temp->essayId == essayId)
            {
                lastTemp->nextEssay = temp->nextEssay;
                free(temp);
                // head->length--;
                return true;
            }
            lastTemp = temp;
        }
    }
    return false;
}
bool ShowAllProject(UserList user)
{
    ProjectList temp = (ProjectList)malloc(sizeof(ProjectList));
    temp = user->nextProject;
    if (user->nextProject == NULL)
    {
        printf("该人员没有所属项目\n");
        return false;
    }
    else
    {
        while (temp->nextProject != NULL)
        {
            printf("所属项目:%s 项目状态:%s 获奖状况:%s\n",
                   temp->projectName,
                   temp->projectStatus == 0 ? "未开始" : temp->projectStatus == 1 ? "进行中" : "已完成",
                   temp->projectAwards == 0 ? "未获奖" : temp->projectAwards == 1 ? "一等奖" : temp->projectAwards == 2 ? "二等奖" : temp->projectAwards == 3 ? "三等奖" : "四等奖");
            temp = temp->nextProject;
        }
        printf("所属项目:%s 项目状态:%s 获奖状况:%s\n",
               temp->projectName,
               temp->projectStatus == 0 ? "未开始" : temp->projectStatus == 1 ? "进行中" : "已完成",
               temp->projectAwards == 0 ? "未获奖" : temp->projectAwards == 1 ? "一等奖" : temp->projectAwards == 2 ? "二等奖" : temp->projectAwards == 3 ? "三等奖" : "四等奖");
    }
    return true;
}
bool ShowAllThesis(UserList user)
{
    ThesisList temp = (ThesisList)malloc(sizeof(ThesisList));
    temp = user->nextThesis;
    if (temp == NULL || temp->nextThesis == NULL)
    {
        printf("该人员没有论文\n");
        return false;
    }
    else
    {
        temp = temp->nextThesis;
        while (temp->nextThesis != NULL)
        {
            printf("论文名称:%s 论文ID:%ld 论文状态:%s 获奖情况%s 论文类型%s \n",
                   temp->thesisName,
                   temp->thesisId,
                   temp->thesisStatus == 0 ? "未开始" : temp->thesisStatus == 1 ? "进行中" : "已完成",
                   temp->thesisAwards == 0 ? "未获奖" : temp->thesisAwards == 1 ? "国家级" : temp->thesisAwards ? "省部级" : temp->thesisAwards == 2 ? "市厅级" : "校级",
                   temp->thesisType == 0 ? "SCI" : temp->thesisType == 1 ? "EI" : temp->thesisType == 2 ? "核心" : "一般");
        }
        printf("论文名称:%s 论文ID:%ld 论文状态:%s 获奖情况%s 论文类型%s \n",
               temp->thesisName,
               temp->thesisId,
               temp->thesisStatus == 0 ? "未开始" : temp->thesisStatus == 1 ? "进行中" : "已完成",
               temp->thesisAwards == 0 ? "未获奖" : temp->thesisAwards == 1 ? "国家级" : temp->thesisAwards ? "省部级" : temp->thesisAwards == 2 ? "市厅级" : "校级",
               temp->thesisType == 0 ? "SCI" : temp->thesisType == 1 ? "EI" : temp->thesisType == 2 ? "核心" : "一般");
    }

    return true;
}
bool ShowAllEssay(UserList user)
{
    EssayList temp = (EssayList)malloc(sizeof(EssayList));
    temp = user->nextEssay;
    if (user->nextEssay == NULL)
    {
        printf("该人员没有引用的著作\n");
        return false;
    }
    else
    {
        while (temp->nextEssay != NULL)
        {
            printf("著作名称:%s 著作ID:ld% 著作类型%s\n",
                   temp->essayName,
                   temp->essayId,
                   temp->essayType == 0 ? "无" : temp->essayType == 1 ? "国家标准" : temp->essayType == 2 ? "行业标准" : temp->essayType == 3 ? "专辑" : temp->essayType == 4 ? "学术专著" : "其他(含译著)");
        }
        printf("著作名称:%s 著作ID:ld% \n",
               temp->essayName,
               temp->essayId,
               temp->essayType == 0 ? "无" : temp->essayType == 1 ? "国家标准" : temp->essayType == 2 ? "行业标准" : temp->essayType == 3 ? "专辑" : temp->essayType == 4 ? "学术专著" : "其他(含译著)");
    }

    return (true);
}
bool ShowAllUser(UserList head)
{
    UserList temp = (UserList)malloc(sizeof(User));
    temp = head;
    if (head->nextUser == NULL)
    {
        printf("表不存在，查询失败");
        return false;
    }
    else
    {
        while (temp->nextUser != NULL)
        {
            temp = temp->nextUser;
            printf("ID:%ld 名字:%s \n", temp->id, temp->name);
            //在输出项目等数据之前输出一下名字，可能会删
            printf("%s \n", temp->name);
            ShowAllProject(temp);
            ShowAllThesis(temp);
            ShowAllEssay(temp);
        }
        return true;
    }
}
bool ShowUserAll(UserList user)
{
    if (user == NULL)
    {
        return false;
    }
    UserList temp = (UserList)malloc(sizeof(User));
    temp = user;
    printf("ID:%ld 名字:%s \n", temp->id, temp->name);
    //在输出项目等数据之前输出一下名字，可能会删
    printf("%s \n", temp->name);
    ShowAllProject(temp);
    ShowAllThesis(temp);
    ShowAllEssay(temp);
    return true;
}
bool AddRank(RankList rankHead, Rank rank0)
{
    if (rankHead == NULL)
    {
        return false;
    }
    RankList lastTemp = (RankList)malloc(sizeof(Rank));
    RankList temp = (RankList)malloc(sizeof(Rank));
    RankList rank = (RankList)malloc(sizeof(Rank));
    strcpy(rank->name, rank0.name);
    rank->score = rank0.score;
    if (rankHead->nextRank == NULL)
    {
        rankHead->nextRank = rank;
        rank->nextRank = NULL;
    }
    else
    {
        lastTemp = rankHead;
        temp = rankHead->nextRank;
        while (temp->nextRank != NULL)
        {
            if (temp->score<=rank->score)
            {
                lastTemp->nextRank = rank;
                rank->nextRank = temp;
                return true;
            }
            lastTemp = lastTemp->nextRank;
            temp = temp->nextRank;
        }
        temp->nextRank = rank;
        rank->nextRank = NULL;
    }
    return true;
}
void ShowRank(UserList head, RankList rankHead)
{
    UserList temp = (UserList)malloc(sizeof(User));
    RankList rank = (RankList)malloc(sizeof(Rank));
    RankList showRank = (RankList)malloc(sizeof(Rank));
    temp = head;
    int i = 0;
    if (head->nextUser == NULL)
    {
        printf("表不存在，查询失败");
    }
    else
    {
        while (temp->nextUser != NULL)
        {
            temp = temp->nextUser;
            ProjectList project = (ProjectList)malloc(sizeof(Project));
            ThesisList thesis = (ThesisList)malloc(sizeof(Thesis));
            EssayList essay = (EssayList)malloc(sizeof(Essay));
            int score = 0;
            if (temp->nextProject != NULL)
            {
                project = temp->nextProject;
                score += project->projectAwards == 1 ? 500 : project->projectAwards == 2 ? 300 : project->projectAwards == 3 ? 100 : 0;
                while (project->nextProject != NULL)
                {
                    project = project->nextProject;
                    score += project->projectAwards == 1 ? 500 : project->projectAwards == 2 ? 300 : project->projectAwards == 3 ? 100 : 0;
                }
            }
            if (temp->nextThesis != NULL)
            {
                thesis = thesis->nextThesis;
                score += thesis->thesisType == 1 ? 500 : thesis->thesisType == 2 ? 300 : thesis->thesisType == 3 ? 100 : thesis->thesisType == 4 ? 50 : 0;
                while (thesis->nextThesis != NULL)
                {
                    thesis = thesis->nextThesis;
                    score += thesis->thesisType == 1 ? 500 : thesis->thesisType == 2 ? 300 : thesis->thesisType == 3 ? 100 : thesis->thesisType == 4 ? 50 : 0;
                }
            }
            if (temp->nextEssay != NULL)
            {
                essay = temp->nextEssay;
                score += essay->essayType == 1 ? 500 : essay->essayType == 2 ? 300 : essay->essayType == 3 ? 200 : essay->essayType == 4 ? 100 : essay->essayType == 5 ? 50 : 0;
                while (essay->nextEssay != NULL)
                {
                    essay = temp->nextEssay;
                    score += essay->essayType == 1 ? 500 : essay->essayType == 2 ? 300 : essay->essayType == 3 ? 200 : essay->essayType == 4 ? 100 : essay->essayType == 5 ? 50 : 0;
                }
            }
            rank->score = score;
            strcpy(rank->name, temp->name);
            if (AddRank(rankHead, *rank))
            {
                i++;
                continue;
            }
        }
        // BubbleSort(rankHead);
        showRank = rankHead;
        int i = 0;
        while (showRank->nextRank != NULL)
        {
            i++;
            showRank = showRank->nextRank;
            printf("排名:%d  姓名:%s  得分:%d\n",i,showRank->name,showRank->score);
        }
    }
}

bool ModifyUser(UserList user,char name[10])
{
    strcpy(user->name,name);
}
bool ModifyProject(ProjectList project,long projectId, char projectName[999], short projectStatus, short projectAwards)
{
    if (projectName != NULL)
    strcpy(project->projectName,projectName);
    if (projectStatus != NULL)
    project->projectStatus = projectStatus;
    if (projectAwards != NULL)
    project->projectAwards = projectAwards;
}
bool ModifyThesis(ThesisList thesis,long thesisId, char thesisName[999], short thesisStatus, short thesisAwards, short thesisType)
{
    if (thesisName != NULL)
    strcpy(thesis->thesisName,thesisName);
    if (thesisStatus != NULL)
    thesis->thesisStatus = thesisStatus;
    if (thesisAwards != NULL)
    thesis->thesisAwards = thesisAwards;
    if (thesisType != NULL)
    thesis->thesisType = thesisType;
}
bool ModifyEssay(EssayList essay,long essayId, char essayName[999], short essayType)
{
    if (essayName != NULL)
    strcpy(essay->essayName,essayName);
    if (essayName != NULL)
    strcpy(essay->essayType,essayType);
}
void Menu()
{
    system("color A0"); //更改主菜单颜色
    printf("欢迎使用学生信息管理系统！\n");
    printf("\t\t\t\t================================================\n");
    printf("\t\t\t\t| 1:按照学号查询学生信息 | 2:显示所有学生信息  |\n");
    printf("\t\t\t\t| 3:修改或增加学生信息   | 4:删除学生信息      |\n");
    printf("\t\t\t\t| 5:清空所有信息         | 6:把修改保存到数据库|\n");
    printf("\t\t\t\t| 7;从文件中导入信息     | 8:退出系统          |\n");
    printf("\t\t\t\t================================================\n");
    printf("请选择:");
    // Set(); //调用功能选择函数
}
void Login()
{
    char name[] = "00000", pwd[] = "admin"; //正确的用户名和密码
    char username[10], userpwd[100];        //用户输入的用户名和密码
    {
        system("color F1");
        printf("\n\n\n\n\n\n\n\n\n");
        printf("                                             University scientific research management system\n\n");
        printf("                                             欢迎使用高校科研管理系统!\n\n");

    re:
        printf("                                             请输入管理员账号:");
        gets(username);
        printf("\n");
        printf("                                             请输入密码:");
        fflush(stdin); //清理缓存区
        int m;
        for (m = 0; m < 100; m++) //隐藏密码功能
        {
            userpwd[m] = getch(); //读入用户名
            if (userpwd[m] == '\r')
                break;
            if (userpwd[m] == 8 && m > 0) //如果用户按退格键
            {
                printf("\b \b"); //显示退一格
                m = m - 2;       //输入数据退1
                continue;
            }
            if (userpwd[m] == 8 && m == 0) //无格可退时按退格键
            {
                m = m - 1; //输入数据退1
                continue;
            }
            printf("*");
        }
        userpwd[m] = '\0';
        //如果用户名和密码正确,则登录成功,返回1
        printf("\n\n");
        if (strcmp(name, username) == 0 && strcmp(pwd, userpwd) == 0)
        {
            // LoadFromFile(); //读取数据
            system("cls"); //清空屏幕
            Menu();        //进入主页面
        }
        else
        {
            printf("                                             账号或密码错误,请重新输入!\n\n");
            goto re; //重新输入账号密码
        }
    }
}

void InitInformation(UserList head){
    char *first = {"张李王孙曹"};
    char *girl = { "秀娟英华慧巧美娜静淑"};
    char *boy = {"伟刚勇毅俊峰强军平保"};
    for (int i = 0; i < 10; i++)
    {
        /* code */
    }
    
}

int main(int argc, char const *argv[])

{

    UserList head;
    RankList rankHead;
    char name[10] = "fadsf";
    char essayName[999] = "trweyusfdghfhjdfghj";
    head = InitUser();
    rankHead = InitRank();
    system("pause");
    return 0;
}